﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Filter;
using Newtonsoft.Json.Linq;
using HttpUtility;
using WechatModel;
using WxPay;

namespace WechatDAL
{
    /// <summary>
    /// 公众号发送消息
    /// </summary>
    public class W_MessageDal
    {
        /// <summary>
        /// 公众号发送消息
        /// </summary>
        /// <param name="touser">发送对象的openid</param>
        /// <param name="content">发送内容</param>
        /// <returns></returns>
        public bool SendMessage(string touser, string content)
        {
            JObject jo = Poster.Post(WechatAPI4Public.Sendmessage, content);
            if (jo == null)
                return false;
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "ok")
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// 公众号发送模版消息
        /// </summary>
        /// <param name="content">发送内容</param>
        /// <returns></returns>
        public bool SendTempMessage(string content)
        {
            JObject jo = Poster.Post(WechatAPI4Public.SendTempMessage, content);
            if (jo == null)
            {
                return false;
            }
            else
            {
                if (jo["errcode"].ToString() == "0" && jo["errmsg"].ToString() == "ok")
                {
                    return true;
                }
                else
                {
                    Log.Error(this.GetType().ToString(), "模版消息发送失败。errcode：" + jo["errcode"].ToString() + "；errmsg：" + jo["errmsg"].ToString());
                    return false;
                }
            }
        }
    }
}
